function hu(counts) {
    for (var i = 0; i < counts.length; i++) {
        if (counts[i] >= 2) {
            counts[i] -= 2;
            if (check(counts)) {
                return true
            }
            counts[i] += 2
        }
    }
    return false
}
function check(counts) {
    for (var i = 0; i < counts.length; i++) {
        if (counts[i] >= 3) {
            counts[i] -= 3;
            if (check(counts)) {
                return true;
            }
            counts[i] += 3;
        }
    }
    for (i = 0; i < 27; i++) {
        if (i % 9 <= 6 && counts[i] >= 1 && counts[i + 1] >= 1 && counts[i + 2] >= 1) {
            counts[i]--;
            counts[i + 1]--;
            counts[i + 2]--;
            if (check(counts)) {
                return true;
            }
            counts[i]++;
            counts[i + 1]++;
            counts[i + 2]++;
        }
    }
    for (i = 0; i < counts.length; i++) {
        if (counts[i] > 0) {
            return false;
        }
    }
    return true;
}
function shuffleRange(array, i0, i1) {
    var m = i1 - (i0 = +i0);
    while (m) {
        var i = Math.random() * m-- | 0, t = array[m + i0];
        array[m + i0] = array[i + i0];
        array[i + i0] = t
    }
    return array;
}
function shuffle(array) {
    var i0 = 0;
    var i1 = array.length;
    return shuffleRange(array, i0, i1);
}
function count(array) {
    var counts = new Array(34);
    counts.fill(0);
    for (var i = 0; i < counts.length; i++) {
        for (var j = 0; j < array.length; j++) {
            if (array[j] === i) {
                counts[i]++;
            }
        }
    }
    return counts
}
function next(directions, current) {
    var index = directions.indexOf(current);
    if (index === directions.length - 1) {
        return directions[0]
    }
    return directions[index + 1]
}
function dispatch(array, host, directions) {
    var playerMap = new Map([
        ['东', {up: new Array(34).fill(0), down: [], discards: []}],
        ['南', {up: new Array(34).fill(0), down: [], discards: []}],
        ['西', {up: new Array(34).fill(0), down: [], discards: []}],
        ['北', {up: new Array(34).fill(0), down: [], discards: []}]
    ]);
    var length = array.length;
    var direction = host;
    while (true) {
        if (array.length <= length - 4 * 13) {
            break
        }
        playerMap.get(direction)['up'][array.shift()]++;
        direction = next(directions, direction)
    }
    return playerMap
}
function obtain(array, player) {
    player['up'].push(array.shift())
}
function discard(player, discard) {
    player['up'][discard]--;
    player['discards'].push(discard)
}
function chOptions(up, discard) {
    var chOptions = [];
    if (discard >= 27) {
        return chOptions
    }
    if (Math.floor((discard - 2)/9) === Math.floor(discard/9) && up[discard - 2] > 0 && up[discard - 1] > 0) {
        chOptions.push([discard - 2, discard - 1, discard])
    }
    if (Math.floor((discard - 1)/9) === Math.floor((discard + 1)/9) && up[discard - 1] > 0 && up[discard + 1] > 0) {
        chOptions.push([discard - 1, discard, discard + 1])
    }
    if (Math.floor(discard/9) === Math.floor((discard + 2)/9) && up[discard + 1] > 0 && up[discard + 2] > 0) {
        chOptions.push([discard, discard + 1, discard + 2])
    }
    return chOptions
}
function ch(player, discard, chOptions, index, discardPlayer) {
    var chOption = chOptions[index];
    player['down'].push(chOption);
    for (var i = 0; i < chOptions.length; i++) {
        if (chOption[i] !== discard) {
            player['up'][discard]--
        }
    }
    discardPlayer['discards'].pop()
}
function canPong(up, discard) {
    return [discard] >= 2
}
function pong(player, discard, discardPlayer) {
    player['up'][discard] -= 2;
    player['down'].push([discard, discard, discard]);
    discardPlayer['discards'].pop()
}
function anGangOptions(up) {
    var anGangs = [];
    for (var i = 0; i < up.length; i++) {
        if (up[i] >= 4) {
            anGangs.push(i)
        }
    }
    return anGangs
}
function anGang(player, anGangs, index) {
    var card = anGangs[index];
    player['up'][card] -= 4;
    player['down'].push([card, card, card, card]);
}
function gangOptions(player) {
    var gangs = [];
    var down = player['down'];
    for (var i = 0; i < down.length; i++) {
        if (down[i][0] === down[i][1] && down[i].length === 3 && player['up'][down[i][0]] === 1) {
            gangs.push(down[i][0])
        }
    }
    return gangs
}
function gang(player, gangs, index) {
    var card = gangs[index];
    var down = player['down'];
    for (var i = 0; i < down.length; i++) {
        if (down[i][0] === card && down[i].length === 3) {
            player['up'][card]--;
            down[i].push(card)
        }
    }
}
function canGang(up, discard) {
    return [discard] >= 3
}
function discardGang(player, discard, discardPlayer) {
    player['up'][discard] -= 3;
    player['down'].push([discard, discard, discard, discard]);
    discardPlayer['discards'].pop()
}